(function($) {
	var tempArr = [];
	var $this;
	var $select;
    var $input;
   //两个字符串的相似程度，并返回相似度百分比
    var strSimilarity2Percent = function (x, y) {
	    var z = 0;  
	    var s = x.length + y.length;;  
	  
	    x.sort();  
	    y.sort();  
	    var a = x.shift();  
	    var b = y.shift();  
	  
	    while(a !== undefined && b !== undefined) {  
	        if (a === b) {  
	            z++;  
	            a = x.shift();  
	            b = y.shift();  
	        } else if (a < b) {  
	            a = x.shift();  
	        } else if (a > b) {  
	            b = y.shift();  
	        }  
	    }  
	    return z/s * 200;  
    };
    var sort = function (arr,val){
        for(i=0;i<arr.length-1;i++){
            for(j=0;j<arr.length-1-i;j++){
                if(strSimilarity2Percent(arr[j],val)>strSimilarity2Percent(arr[j+1]),val){
                    var temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
            }
        }
        return arr;
    }
	var setinput = function(){
    	$select.html("");
    	var opt = "";
        for(i=0;i<tempArr.length;i++){
            if(tempArr[i].name.indexOf($input.val()) > -1){
            	opt += "<option value = '" + tempArr[i].value + "'>" + tempArr[i].name + "</option>";
            } 
        } 
        $select.append(opt); 
    }
	var init = function(){
		var dataUrl = $this.attr("dataUrl");
		var lablename = $this.attr("lablename");
		var valuename = $this.attr("valuename");
		var dataname = $this.attr("dataname");
		var datavalue = $this.attr("datavalue");
		var _inputContent=  '<input autocomplete="off" type="text" name="' + dataname +'" class="makeinp"  placeholder="请选择或输入">';
        var _selectContent= '<select name = "' + datavalue + '" size="9" style="display:none;"></select>';
        $this.append(_inputContent);
        $this.append(_selectContent);
        $select = $this.find("select");
        $input = $this.find("input");
        $.ajax({
	        type: 'post',
	        url:baseUrl + dataUrl,
	        contentType: false,  
	        processData: false, 
	        async: false,
		}).done(function(data){
			var l = data.length;
			tempArr = [];
			var opt = "";
			for(var index = 0 ; index < l ; index++){
				var temp = {};
				opt += "<option value = '" + data[index][valuename] + "'>" + data[index][lablename] + "</option>";
				temp.name = data[index][lablename];
				temp.value = data[index][valuename];
				tempArr.push(temp);
			}
			$select.append(opt);
		});
	}
	$.fn.getVal = function(){
		return $select.val();
	}
	$.fn.initSelect = function(){
		$this = $(this);
		init();
        $input.focus(function(){
        	$select.css({"display":""});  
        });
        $input.click(function(){
        	$select.css({"display":""});
        });
        $input.bind('input propertychange',setinput);
        $select.change(function(){
        	$(this).prev("input").val($(this).find("option:selected").text());  
            $(this).css({"display":"none"});
        });
        $(document).bind('click', function(e) {  
            var e = e || window.event;  
            var elem = e.target || e.srcElement;  
            while (elem) {  
                if (elem == $select[0] || elem == $input[0]) {  
                    return;  
                }  
                elem = elem.parentNode;  
            }  
            $select.css('display', 'none');  
        });
	}
})(jQuery);    
$(document).ready(function(){
	$("div.fuzzy-query").initSelect();
})
// 模糊查询
   /* var TempArr=[];  
    $(function(){  
        var _fuzzyQuery=$('.fuzzy-query');
        var _inputContent=  '<input type="text" name="makeupCo" id="makeupCo" class="makeinp" onfocus="setfocus(this)" oninput="setinput(this)" placeholder="请选择或输入">';
        var _selectContent= '<select name="makeupCoSe" id="typenum" onchange="changeF(this)" size="10" style="display:none;"></select>';
        var _optionContent= '<option value="">1印刷厂</option>'+  
                            '<option value="">12印刷厂</option>'+ 
                            '<option value="">123印刷厂</option>'+  
                            '<option value="">1234印刷厂</option>'+  
                            '<option value="">12345印刷厂</option>'+  
                            '<option value="">123456印刷厂</option>'+  
                            '<option value="">1234567印刷厂</option>'+  
                            '<option value="">12345678印刷厂</option>';
            _fuzzyQuery.append(_inputContent);
            _fuzzyQuery.append(_selectContent);
            $('#typenum').append(_optionContent);

        $("#typenum option").each(function(index, el) {  
            TempArr[index] = $(this).text();  
        });  
        $(document).bind('click', function(e) {  
            var e = e || window.event;  
            var elem = e.target || e.srcElement;  
            while (elem) {  
                if (elem.id && (elem.id == 'typenum' || elem.id == "makeupCo")) {  
                    return;  
                }  
                elem = elem.parentNode;  
            }  
            $('#typenum').css('display', 'none');  
        });  
    })  

    function changeF(this_) {  
    }  

    function setfocus(this_){  
        $("#typenum").css({"display":""});  
        var select = $("#typenum");  
        select.empty();  
        setinput(this_);  
        if(!select){  
            for(i=0;i<TempArr.length;i++){  
                var option = $("<option></option>").text(TempArr[i]);  
                select.append(option);  
            }  
        }  
    }  
    function setinput(this_){  
        var select = $("#typenum");  
        select.html("");  
        for(i=0;i<TempArr.length;i++){  
            if(TempArr[i].substring(0,this_.value.length).indexOf(this_.value)==0){  
                var option = $("<option></option>").text(TempArr[i]);  
                select.append(option);  
            }  
        }  
    }  */

